﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class DiagnoseTableForm : Form
    {
        List<Diagnose> diagList = new List<Diagnose>();

        public DiagnoseTableForm()
        {
            InitializeComponent();
            SetDataToGrid();
        }

        public void FillRow(int i)
        {
            diagGrid.Rows.Add();
            Diagnose d = diagList[i];
            diagGrid.Rows[i].Cells[0].Value = d.name;
            diagGrid.Rows[i].Cells[1].Value = d.description;
        }

        public void SetDataToGrid()
        {
            diagList = DiagnoseDB.GetDiagnoses();
            for (int i = 0; i < diagList.Count; i++)
            {
                FillRow(i);
            }
        }

        public Diagnose FormDiagnoseFromDataGrid()
        {
            Diagnose diag = new Diagnose();
            diagList = DiagnoseDB.GetDiagnoses();
            for (int i = 0; i < diagList.Count; i++)
            {
                if (diagGrid.IsCurrentCellInEditMode)
                {
                    diag.name = diagGrid.Rows[i].Cells[0].Value.ToString();
                    diag.description = diagGrid.Rows[i].Cells[1].Value.ToString();

                }
            }

            return diag;
        }


        private void AddBtn_Click(object sender, EventArgs e)
        {
            Diagnose diag = new Diagnose();
            DiagnoseDetailsForm detForm = new DiagnoseDetailsForm(diag);

            if (detForm.ShowDialog() == DialogResult.OK)
            {
                DiagnoseDB.SaveDiagnoseToDB(diag);
                diagList.Add(diag);
                FillRow(diagList.Count - 1);
                MessageBox.Show("New diagnose added!");
            }
        }

        private void ParseDataToDiagnoseDetailsFom()
        {
            int index = diagGrid.CurrentRow.Index;
            List<Diagnose> lines = DiagnoseDB.GetDiagnoses();
            Diagnose diag = lines[index];
            DiagnoseDetailsForm diagDet = new DiagnoseDetailsForm(diag);
            
            
            //diagGrid.Rows[index].Cells[0].Value.ToString() = diagDet.Name;
            
        }

        private void EditBtn_Click(object sender, EventArgs e)
        {
            int index = diagGrid.CurrentRow.Index;
            Diagnose editedDiag = diagList[index];
            DiagnoseDetailsForm detForm = new DiagnoseDetailsForm(editedDiag);

            if (detForm.ShowDialog() == DialogResult.OK)
            {
                DiagnoseDB.EditedDiagnoseToDB(index, editedDiag);
                List<Diagnose> lines = DiagnoseDB.GetDiagnoses();
                foreach (Diagnose d in diagList)
                {
                    if (d.ID == editedDiag.ID)
                    {
                        DiagnoseDB.SaveDiagnoseToDB(editedDiag, lines);
                        FillRow(index);
                        MessageBox.Show("Diagnose edited!");
                    }
            }
                }
        }

        private void DeleteBtn_Click(object sender, EventArgs e)
        {
            int index = diagGrid.CurrentRow.Index;

            if (MessageBox.Show("Are you sure you want to delete diagnose?", "Delete diagnose", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                DiagnoseDB.DeleteDiagnose(index);
                diagList.RemoveAt(index);
                diagGrid.Rows.RemoveAt(index);
                MessageBox.Show("Diagnose deleted!");
            }

        }

        private void DiagnoseTableForm_Load(object sender, EventArgs e)
        {

        }

    }

}

